package com.goldgov.statistics.zbqchart.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.branchCircle.service.BranchCircle;
import com.goldgov.branchCircle.service.BranchCircleQuery;
import com.goldgov.branchCircle.service.BranchCircleService;
import com.goldgov.branchCircle.service.OrganArticleQuery;
import com.goldgov.branchCircle.service.OrganizationEntity;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench/statistics/zbqchart")
@Api("支部圈统计接口")
public class ZbqChartController {
	@Autowired
	private BranchCircleService branchCircleService;
	
	@GetMapping("/findBranchCircleByPerson")
	@ApiOperation(value = "支部圈个人发布数", notes = "支部圈个人发布数")
	public JsonQueryObject<BranchCircle> findBranchCircleByPerson( BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<BranchCircle> list=branchCircleService.findBranchCircleByPerson(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/findBranchCircleByPersonWeek")
	@ApiOperation(value = "支部圈个人月每周发布数", notes = "支部圈个人月每周发布数")
	public JsonQueryObject<BranchCircle> findBranchCircleByPersonWeek( BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<BranchCircle> list=branchCircleService.findBranchCircleByPersonWeek(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findBranchCircleByOrgAvg")
	@ApiOperation(value = "支部圈发布平均数", notes = "支部圈发布平均数")
	public JsonQueryObject<BranchCircle> findBranchCircleByOrgAvg( BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		if (query.getSearchYear()  == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<BranchCircle> list=branchCircleService.findBranchCircleByOrgAvg(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}	
	
	
	@GetMapping("/findMyBranchCirclePerson")
	@ApiOperation(value = "个人所在支部圈每人发布数", notes = "个人所在支部圈每人发布数")
	public JsonQueryObject<BranchCircle> findMyBranchCirclePerson( BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		SwbAuthUser  user=LoginAuthUser.getUserByAdmin(request);
		BranchCircle bean=branchCircleService.findOrgByUserId(user.getEntityID());
		query.setSearchOrgID(bean.getOrgID());
		List<BranchCircle> list=branchCircleService.findBranchCircleByPerson(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/findMyBranchCirclWeek")
	@ApiOperation(value = "个人所在支部圈本周每人发布数", notes = "个人所在支部圈本周每人发布数")
	public JsonQueryObject<BranchCircle> findMyBranchCirclWeek( BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		SwbAuthUser  user=LoginAuthUser.getUserByAdmin(request);
		BranchCircle bean=branchCircleService.findOrgByUserId(user.getEntityID());
		query.setSearchOrgID(bean.getOrgID());
		List<BranchCircle> list=branchCircleService.findMyBranchCirclWeek(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findBranchCircleByOrg")
	@ApiOperation(value = "统计组织机构下一级发布总数", notes = "统计组织机构下一级发布总数")
	public JsonQueryObject<OrganizationEntity> findBranchCircleByOrg( OrganArticleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (PropertyUtil.objectNotEmpty(query.getSearchOrgID())) {
			List<OrganizationEntity> findChildOrganization = branchCircleService.findChildOrganization(query.getSearchOrgID());
			if (findChildOrganization.size()>0) {
				String valueOf = String.valueOf(DateUtil.getYear(new Date()));
				for (OrganizationEntity organizationEntity : findChildOrganization) {
					Integer userNum=0;
					Integer pageNum=0;
					BranchCircleQuery branchCircleQuery = new BranchCircleQuery();
					if (PropertyUtil.objectNotEmpty(query.getSearchYear())) {
						branchCircleQuery.setSearchYear(String.valueOf(query.getSearchYear()));
					}else {
						branchCircleQuery.setSearchYear(valueOf);
					}
					branchCircleQuery.setSearchScopeCode(organizationEntity.getScopeCode());
					BeanUtils.copyProperties(query, branchCircleQuery);
					List<BranchCircle> list=branchCircleService.findBranchCircleByOrgAvg(branchCircleQuery);
					if (list.size()>0) {
						for (BranchCircle branchCircle : list) {
							userNum+=branchCircle.getUserNum();
							pageNum+=branchCircle.getArticleNum();
						}
						organizationEntity.setAirtcleNum(pageNum);
						organizationEntity.setUserNum(userNum);
					}
				}
			}
			query.setResultList(findChildOrganization);
		}
		return new JsonQueryObject<>(query);
	}
	
	
	
	@GetMapping("/countOrgBranchByTime")
	@ApiOperation(value = "获取组织下特定时间段支部圈发布数", notes = "获取组织下特定时间段支部圈发布数")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchTreePath", value = "查询路径", paramType = "query"),
		 @ApiImplicitParam(name = "searchYear", value = "年份", paramType = "query"),
		 @ApiImplicitParam(name = "searchDateType", value = "时间类别", paramType = "query"),
		 @ApiImplicitParam(name = "searchBeginTime", value = "开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchEndTime", value = "结束时间", paramType = "query")
	})
	public JsonQueryObject<BranchCircle> countOrgBranchByTime(@ApiIgnore BranchCircleQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		if(PropertyUtil.objectNotEmpty(query.getSearchTreePath())&&PropertyUtil.objectNotEmpty(query.getSearchDateType())) {
			List<BranchCircle> list=branchCircleService.countOrgBranchByTime(query);
			query.setResultList(list);
		}
		return new JsonQueryObject<>(query);
	}
}
